<?php

/**
 * 
 * @author yuxun
 *
 */
class RecourseChannelService extends Service{
	
	public static function indexHeader(){
		return array(
				'id'         => array('text' => 'ID', 'rule' => 'int'),
				'channel'     => array('text' => '渠道'),
				'desc'        => array('text' => '备注'),
				'type'        => array('text' => '类型'),
				'create_time'=> array('text' => '添加时间'),
				'action'     => array('text' => '操作')
		);
	}
	
	public static function indexData($params){
		$modRecourseChannel = new ModRecourseChannel();
		$result = $modRecourseChannel->page($params);
		$typeList = self::typeList();
		$length = count($result['data']);
		foreach($result['data'] as &$v){
			$type = 1;
			if($k == 0 && $length == 1){  //当前页只剩下一条数据时，删除之后需显示上一页的数据
				$type = 2;
			}
			
			$v['type'] = $typeList[$v['type']];
			$v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
			
			$v['action']  .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=recourseChannel/edit').'&id='.$v['id'].'">编辑</a>';
			$v['action']  .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=recourseChannel/delete',$type).'&id='.$v['id'].'" onclick="del()" confirm="是否要删除该条服信息?">删除</a>';
		}
		unset($v);
		return $result;
	}
	
	public static function doAdd($params){
		$user = AccAuth::me();
	
		$modRecourseChannel = new ModRecourseChannel();
		$modRecourseChannel->game          = $params['g'];
		$modRecourseChannel->channel   = $params['channel'];
		$modRecourseChannel->desc  = $params['desc'];
		$modRecourseChannel->type  = $params['type'];
		$modRecourseChannel->create_user   = $user['UserID'];
		$modRecourseChannel->create_time   = time();
		$modRecourseChannel->update_user   = $user['UserID'];
		$modRecourseChannel->update_time   = time();
		$result = $modRecourseChannel->save();
		return array('state' => true, 'msg' => '成功增加资源渠道');
	}
	
	public static function get($id){
		$modRecourseChannel = new ModRecourseChannel();
		$obj = $modRecourseChannel->findByPk($id);
	
		if($obj){
			$info = $obj->getAttributes();
			return $info;
		}else{
			return false;
		}
	}
	
	public static function doEdit($id, $params){
		if(empty($id)) return array('state' => false, 'msg' => '信息不存在');
		$user = AccAuth::me();
		$modRecourseChannel = new ModRecourseChannel();
		$update = array();
	
		$update['channel'] = $params['channel'];
		$update['desc'] = $params['desc'];
		$update['type'] = $params['type'];
		$update['update_user']  = $user['UserID'];
		$update['update_time']  = time();
		 
		$result = $modRecourseChannel->updateByPk($id, $update);

		return true;
	}
	
	public static function delete($id){
		if(empty($id)) return false;		
		$modRecourseChannel = new ModRecourseChannel();
		$modRecourseChannel->deleteByPk($id);
		return array('state' => true, 'msg' => '成功删除资源渠道');
	}
	
	
	/*
	 * 去重
	 */
	public static function repeat($id = 0, $field, $val,$g){
		$modRecourseChannel = new ModRecourseChannel();
		$conut = $modRecourseChannel->repeat($id, $field, $val,$g);
		return $conut;
	}
	
	public static function typeList(){
		return array(
				'1'=>'正式',
				'2'=>'测试',
				'3'=>'审核',
		);
	}


}